package com.fansl.koala.quickdev.module.koala.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fansl.koala.quickdev.module.koala.entity.UserEverydayStatisticDetail;
import com.fansl.koala.quickdev.module.koala.mapper.UserEverydayStatisticDetailMapper;
import com.fansl.koala.quickdev.module.koala.mapper.UserMapper;
import com.fansl.koala.quickdev.module.koala.service.IUserEverydayStatisticDetailService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author fansl
 * @Description: TODO
 * @date 2020/5/6 12:19
 */
@Service
@DS("koala_statistic")
public class UserEverydayStatisticDetailServiceImpl extends ServiceImpl<UserEverydayStatisticDetailMapper, UserEverydayStatisticDetail> implements IUserEverydayStatisticDetailService {
    /**
     * 获取用户vip开通后打卡任务记录
     *
     * @param userId    用户ID
     * @param vipTime vip开通时间
     * @return 用户每日任务打卡记录
     */
    @Override
    public List<UserEverydayStatisticDetail> getUserVipShareInfo(Long userId, LocalDateTime vipTime) {
        String tableName = getTaskName(userId);
        return baseMapper.getUserVipShareInfo(tableName,userId,vipTime);
    }

    /**
     * 获取用户某天的任务完成情况
     *
     * @param userId   用户id
     * @param dateTime 查询的任务日期
     * @return 任务完成情况
     */
    @Override
    public List<UserEverydayStatisticDetail> getTaskInfoInDate(Long userId, DateTime dateTime) {
        String tableName = getTaskName(userId);
        return baseMapper.getTaskInfoInDate(tableName,userId,LocalDateTime.ofInstant(dateTime.toInstant(),dateTime.getZoneId()));
    }

    private String getTaskName(Long userId){
        return String.format("user_everydaytask_day_statistic_detail_%d",
                userId % 16L);
    }
}
